package com.efe.ms.esservice.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.efe.ms.esservice.dao.ProductDocumentRepository;
import com.efe.ms.esservice.model.ProductDocument;

@Service
public class ProductDocumentServiceImpl extends BaseSearchServiceImpl  implements ProductDocumentService {

	
	@Resource
	private  ProductDocumentRepository productDocumentRepository;
	

	 @Resource
	 private ElasticsearchRestTemplate elasticsearchRestTemplate;
	 @Resource
	 private RestHighLevelClient restHighLevelClient;


	@Override
	public void deleteIndex(String indexName) {
		boolean deleteIndex = elasticsearchRestTemplate.deleteIndex(indexName);
		System.out.println("deleteIndex=============>"+deleteIndex);
	}

	@Override
	public void save(ProductDocument productDocuments) {
		productDocumentRepository.save(productDocuments);
	        if(null != productDocuments){
	           System.out.println("保存索引》》》》"+JSON.toJSONString(productDocumentRepository.saveAll(Arrays.asList(productDocuments))));
	        }
		
		
	}

	@Override
	public void delete(String id) {
		 productDocumentRepository.deleteById(id);
	}

	@Override
	public void deleteAll() {
		 productDocumentRepository.deleteAll();
		
	}

	@Override
	public ProductDocument getById(String id) {
		return productDocumentRepository.findById(id).get();
	}

	@Override
	public List<ProductDocument> getAll(int pageindex,int pageSize) {
		Pageable pageable = PageRequest.of(pageindex,pageSize);
		 List<ProductDocument> list = new ArrayList<>();
	     Iterable<ProductDocument> findAll = productDocumentRepository.findAll(pageable);
	     for (ProductDocument productDocument : findAll) {
	    	 list.add(productDocument);
		}
		return list;
	}

	@Override
	public void saveAll(List<ProductDocument> list) {
		productDocumentRepository.saveAll(list);
		
	}

	@Override
	public int getCount() {
		 int count = (int)productDocumentRepository.count();
		return count;
	}

}
